'''
Created on Feb 14, 2014

@author: yfrank
'''

import Project.Simulation.Transport.IOHandler as IOHandler

import Project.Simulation.Transport.PeerTable as PeerTable

import RoboticsPeer

import Project.Main as Main

class Client(object):

    def __init__(self,host):

        self.host = host
        self.peer_table = PeerTable.PeerTable()
        self.iohandler = IOHandler.IOHandler(self.peer_table)


    def connect(self,addr):
        peer = self.peer_table.get_peer(addr)
        if not peer:
            peer = RoboticsPeer.RoboticsPeer(self.host)
        if peer.isClosed(): peer.connect(addr)
        self.peer_table.add_peer(peer,addr)

    def start(self):
        self.iohandler.start()

    def stop(self):
        self.iohandler.stop()

    def send_input(self,path,addr):
        buff = ''
        delim = "|"
        f = open(Main.FILES_PATH+'Workspace.txt','r')
        buff += f.read()+delim
        f.close()
        f = open(Main.FILES_PATH+'Robot.txt','r')
        buff += f.read()+delim
        f.close()
        f = open(Main.FILES_PATH+'Source.txt','r')
        buff += f.read()+delim
        f.close()
        f = open(Main.FILES_PATH+'Target.txt','r')
        buff += f.read()
        f.close()
        self.send(buff,addr)


    def send(self,buff,addr):
        return self.iohandler.send(buff,addr)




